package xhwl.hmo.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.holder.MZHolderCreator;
import xhwl.hmo.mall.holder.MZViewHolder;
import xhwl.hmo.mall.http.bean.resp.RecommendResp;
import xhwl.hmo.mall.ui.SuiteProductsDetailActivity;
import xhwl.hmo.mall.utils.GlideRoundTransform;
import xhwl.hmo.mall.views.MZBannerView;

/**
 * Created by admin on 2017-10-25.
 */

public class RecommendRecycleViewAdapter extends BaseAdapter {
    Context context;
    List<RecommendResp> list;

    public RecommendRecycleViewAdapter(Context context, List<RecommendResp> list) {
        this.context = context;
        this.list = list;

    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_recommend, null);
            holder.recommend_banner = (MZBannerView) convertView.findViewById(R.id.recommend_banner);
            holder.tv_recommendname = (TextView) convertView.findViewById(R.id.tv_recommendname);
            holder.tv_recommendprice = (TextView) convertView.findViewById(R.id.tv_recommendprice);
            holder.tv_recommenddesc = (TextView) convertView.findViewById(R.id.tv_recommenddesc);
            holder.tv_recommenddz_price = (TextView) convertView.findViewById(R.id.tv_recommenddz_price);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (list.get(position).GOODS_MONEY.equals(list.get(position).GOODS_SALE_MONEY)) {
            holder.tv_recommendprice.setVisibility(View.INVISIBLE);
        }
        holder.tv_recommendname.setText(list.get(position).GOODS_NAME);
        holder.tv_recommendprice.setText("¥ " + list.get(position).GOODS_MONEY);
        holder.tv_recommendprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder.tv_recommenddesc.setText(list.get(position).GOODS_DESC);
        holder.tv_recommenddz_price.setText("¥ " + list.get(position).GOODS_SALE_MONEY);

        List<ListObj> lunbolist = new ArrayList();
        for (RecommendResp.LunBo bo : list.get(position).SLIDE) {
            ListObj imageData = new ListObj();
            imageData.position = position;
            imageData.url = bo.PIC_ADD;
            lunbolist.add(imageData);
        }

        holder.recommend_banner.setPages(lunbolist, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        return convertView;
    }

    public class ListObj {
        int position;//保存当前listview position
        String url;//image url
    }

    class BannerViewHolder implements MZViewHolder<ListObj> {
        private ImageView mImageView;

        @Override
        public View createView(final Context context) {
            // 返回页面布局文件
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
            mImageView = (ImageView) view.findViewById(R.id.banner_image);
            return view;
        }

        @Override
        public void onBind(final Context context, final int position, final ListObj data) {
            // 数据绑定
//            mImageView.setImageResource();
            mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, SuiteProductsDetailActivity.class);
                    intent.putExtra("GOOD_ID", list.get(data.position).GOODS_ID);
                    intent.putExtra(SuiteProductsDetailActivity.EXTRA_FROM, 1);
                    intent.putExtra(SuiteProductsDetailActivity.EXTRA_POSITION, position);
                    context.startActivity(intent);
                    //当前context是在fragment中使用的,所以传过来的为fragmentactivity,主要用于页面跳转的方式设置
                    FragmentActivity actcontext;
                    actcontext = (FragmentActivity) context;
                    actcontext.overridePendingTransition(R.anim.outtrans_in_frombottom, R.anim.outtrans_out_fromup);
                }
            });
            Glide.with(context).load(data.url).transform(new GlideRoundTransform(context)).placeholder(R.drawable.load).error(R.drawable.loadfail).into(mImageView);
            ;

        }
    }

    class ViewHolder {
        MZBannerView recommend_banner;
        TextView tv_recommendname, tv_recommendprice, tv_recommenddesc, tv_recommenddz_price;
    }
}
